package com.todo.user.application.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.user.api.UserInfoApi;
import com.todo.user.api.response.BaseUserInfoResponse;
import com.todo.user.api.response.BatchQuerySimplyUserInfoResponse;
import com.todo.user.application.convert.UserInfoConvert;
import com.todo.user.application.entity.bo.UserSimplyInfoBO;
import com.todo.user.application.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2025/2/25 17:04
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/api/userInfo")
@Tag(name = "服务端-用户账号接口", description = "服务端-用户账号接口")
public class UserInfoApiController implements UserInfoApi {

    @Resource
    private UserInfoService userInfoService;

    @Override
    @Operation(summary = "根据用户id查询用户基础信息")
    @PostMapping("/queryBaseUserAccountInfoByUserId")
    public BizResponse<BaseUserInfoResponse> queryBaseUserAccountInfoByUserId(@RequestBody Long userId) {
        if (userId == null || userId < 0) {
            return BizResponse.paramError();
        }
        UserSimplyInfoBO userSimplyInfoBO = userInfoService.querySimplyUserInfoById(userId);
        return BizResponse.success(UserInfoConvert.convertToBaseUserInfoResponse(userSimplyInfoBO));
    }

    @Override
    @Operation(summary = "根据用户idList查询用户基础信息")
    @PostMapping("/batchQuerySimplyUserInfo")
    public BizResponse<BatchQuerySimplyUserInfoResponse> batchQuerySimplyUserInfo(@RequestBody List<Long> userIdList) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return BizResponse.paramError();
        }
        List<Long> collect = userIdList.stream().distinct().collect(Collectors.toList());
        List<UserSimplyInfoBO> userSimplyInfoBOS = userInfoService.batchQuerySimplyUserInfo(collect);
        BatchQuerySimplyUserInfoResponse response = new BatchQuerySimplyUserInfoResponse();
        if (CollectionUtils.isEmpty(userSimplyInfoBOS)) {
            response.setMissedUsrId(collect);
            return BizResponse.success(response);
        }

        List<Long> failedUserIdList = new ArrayList<>();
        Map<Long, UserSimplyInfoBO> userSimplyInfoBOMap = userSimplyInfoBOS
                .stream()
                .collect(Collectors.toMap(UserSimplyInfoBO::getUserId, Function.identity()));

        Map<Long, BatchQuerySimplyUserInfoResponse.SimplyUserInfo> simplyUserInfoMap = new HashMap<>();
        for (Long userId : collect) {
            UserSimplyInfoBO userSimplyInfoBO = userSimplyInfoBOMap.get(userId);
            if (userSimplyInfoBO == null) {
                failedUserIdList.add(userId);
                continue;
            }
            BatchQuerySimplyUserInfoResponse.SimplyUserInfo simplyUserInfo = new BatchQuerySimplyUserInfoResponse.SimplyUserInfo();
            simplyUserInfo.setUserId(userId);
            simplyUserInfo.setUserName(userSimplyInfoBO.getUserName());
            simplyUserInfo.setUserNikeName(userSimplyInfoBO.getNickName());
            simplyUserInfo.setGender(userSimplyInfoBO.getGender());
            simplyUserInfo.setUserAvatar(userSimplyInfoBO.getHeadImg());
            simplyUserInfo.setLocation(userSimplyInfoBO.getLocation());
            simplyUserInfoMap.put(userId, simplyUserInfo);
        }
        if (CollectionUtils.isNotEmpty(failedUserIdList)) {
            log.info("UserInfoApiController#batchQuerySimplyUserInfo 批量查询用户信息，部分数据缺失 failedUserIdList：【{}】", JSONObject.toJSONString(failedUserIdList));
        }
        response.setMissedUsrId(failedUserIdList);
        response.setSimplyUserInfoMap(simplyUserInfoMap);
        return BizResponse.success(response);
    }
}
